package com.zrar.ak.gkpt.service.impl.manage;

import com.alibaba.druid.util.StringUtils;
import com.zrar.ak.gkpt.dao.IManageRoleFieldCorrelationDao;
import com.zrar.ak.gkpt.dao.IManageRoleInfoDao;
import com.zrar.ak.gkpt.model.bo.*;

import com.zrar.ak.gkpt.model.vo.RoleFileVO;
import com.zrar.ak.gkpt.model.vo.UserVO;
import com.zrar.ak.gkpt.service.manage.IManageRoleInfoService;
import com.zrar.ak.gkpt.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ManageRoleInfoService
 * @Description 角色管理Service层
 * @Author liyang
 * @Time 2020/04/03
 */
@Service
@Slf4j
public class ManageRoleInfoService implements IManageRoleInfoService {

    @Autowired
    private IManageRoleInfoDao manageRoleInfoDao;
    @Autowired
    private IManageRoleFieldCorrelationDao manageRoleFieldCorrelationDao;

    @Override
    public JsonResult cudRoleInfo(JsonResult jsonResult, String formDataJson, UserVO userVO) throws Exception {
        ManageRoleInfoBO manageRoleInfoBO = CommonTools.jsonStrToObject(formDataJson, ManageRoleInfoBO.class);
        if(StringUtils.isEmpty(manageRoleInfoBO.getId())){
            //1.数据新增
            manageRoleInfoBO.setId(CommonTools.getUUID());
            manageRoleInfoBO.setCreatorId(userVO.getUserId());
            manageRoleInfoBO.setCreateTime(new Date());
            manageRoleInfoBO.setUpdateUserId(userVO.getUserId());
            manageRoleInfoBO.setUpdateTime(manageRoleInfoBO.getCreateTime());
            manageRoleInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            manageRoleInfoDao.insert(manageRoleInfoBO);
        }else if(BusinessEnum.IS_DELETE_Y.getValue().equals(manageRoleInfoBO.getIsDelete())){
            //2.数据逻辑删除
            manageRoleInfoBO.setUpdateUserId(userVO.getUserId());
            manageRoleInfoBO.setUpdateTime(new Date());
            manageRoleInfoDao.updateByPrimaryKey(manageRoleInfoBO);
        }else{
            //3.数据修改
            manageRoleInfoBO.setUpdateUserId(userVO.getUserId());
            manageRoleInfoBO.setUpdateTime(new Date());
            manageRoleInfoBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            manageRoleInfoDao.updateByPrimaryKey(manageRoleInfoBO);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    @Override
    public JsonPageResult getRoleInfoList(JsonPageResult jsonResult, int currentPage, int pageSize, String roleName) {
        if (currentPage <= 0) {
            currentPage = 1;
        }
        if(StringUtils.isEmpty(roleName)){
            roleName = null;
        }
        Map map = new HashMap();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("roleName",roleName);

        //返回前端的具体的数据
        List list = null;
        //符合展示条件的数据总数
        Integer total = null;

        total = manageRoleInfoDao.queryRoleInfoShowNum(map);
        list = manageRoleInfoDao.pagingQueryRoleInfoList(map);
        if(total==null){
            total=0;
        }
        //计算一共多少页
        Integer pageNum = (total%pageSize==0)?(total/pageSize):(total/pageSize+1);

        jsonResult.setData(list);
        jsonResult.setCurrentPage(currentPage);
        jsonResult.setPageSize(pageSize);
        jsonResult.setTotalCount(total);
        jsonResult.setTotalPage(pageNum);
        if(list == null || list.size()==0){
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_NONEXISTENT_DATA);
        }else{
            CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        }
        return jsonResult;
    }

    @Override
    public JsonResult getRoleFieldInfoListByRoleId(JsonResult jsonResult, String roleId) {
        List<RoleFileVO> roleFileVOList = manageRoleInfoDao.getRoleFieldInfoListByRoleId(roleId);
        for(RoleFileVO roleFileVO : roleFileVOList){
            roleFileVO.setRoleId(roleId);
        }
        jsonResult.setData(roleFileVOList);
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }

    @Override
    @Transactional
    public JsonResult updateRoleFieldInfo(JsonResult jsonResult, String formDataJson, String roleId, UserVO userVO) throws Exception {
        List<ManageFieldInfoBO> manageFieldInfoBOList = (List<ManageFieldInfoBO>) CommonTools.jsonStrToList(formDataJson, ManageFieldInfoBO.class);
        //先根据角色id将角色数据库表字段属性关联关系删除
        manageRoleInfoDao.deleteRoleFileCorrelationByRoleId(roleId);
        //再插入指定角色id对应的数据库表字段属性的关联关系
        Date currentTime = new Date();
        for(ManageFieldInfoBO manageFieldInfoBO : manageFieldInfoBOList){
            ManageRoleFieldCorrelationBO manageRoleFieldCorrelationBO = new ManageRoleFieldCorrelationBO();
            manageRoleFieldCorrelationBO.setId(CommonTools.getUUID());
            manageRoleFieldCorrelationBO.setCreatorId(userVO.getUserId());
            manageRoleFieldCorrelationBO.setCreateTime(currentTime);
            manageRoleFieldCorrelationBO.setUpdateUserId(userVO.getUserId());
            manageRoleFieldCorrelationBO.setUpdateTime(currentTime);
            manageRoleFieldCorrelationBO.setIsDelete(BusinessEnum.IS_DELETE_N.getValue());
            manageRoleFieldCorrelationBO.setRoleId(roleId);
            manageRoleFieldCorrelationBO.setFieldId(manageFieldInfoBO.getId());
            manageRoleFieldCorrelationBO.setRoleFieldPower(manageFieldInfoBO.getRoleFieldPower());
            manageRoleFieldCorrelationDao.insert(manageRoleFieldCorrelationBO);
        }
        CommonTools.jsonResultSetParameter(jsonResult,JsonResultEnum.CODE_SUCCESS);
        return jsonResult;
    }
}
